home *** CD-ROM | disk | FTP | other *** search
/ IRIS Performer 2.2 Friends Demo / SGI IRIS Performer 2.2 Friends Demo.iso / friends / openworlds / tix / Variable.tcl < prev    next >
Text File  |  1997-11-22  |  2KB  |  98 lines

  1. # Variable.tcl
  2. #
  3. #    Routines in this file are used to set up and operate variables
  4. #    for classes that support the -variable option
  5. #
  6. #
  7. #
  8. # tixVariable is a compositional class, this the ":" infix
  9. #
  10.  
  11. # tixVariable:ConfigVariable --
  12. #
  13. #     Set up the -variable option for the object $w
  14. #
  15. # Side effects:
  16. #
  17. #    data(-variable) is changed to the name of the global variable
  18. #    if the global variable exists, data(-value) takes the value of this
  19. #    variable.
  20. #    if the global variable does not exist, it is created with the
  21. #    current data(-value)
  22. #
  23. # Return value:
  24. #
  25. #    true is data(-value) is changed, indicating that data(-command)
  26. #    should be invoked.
  27. #
  28. proc tixVariable:ConfigVariable {w arg} {
  29.     upvar #0 $w data
  30.  
  31.     set changed 0
  32.  
  33.     if {$data(-variable) != {}} {
  34.     uplevel #0 \
  35.         [list trace vdelete $data(-variable) w "tixVariable:TraceProc $w"]
  36.     }
  37.  
  38.     if {$arg != {}} {
  39.     if [uplevel #0 info exists [list $arg]] {
  40.         # This global variable exists, we use its value
  41.         #
  42.         set data(-value) [uplevel #0 set [list $arg]]
  43.         set changed 1
  44.     } else {
  45.         # This global variable does not exist; let's set it 
  46.         #
  47.         uplevel #0 [list set $arg $data(-value)]
  48.     }
  49.     uplevel #0 \
  50.         [list trace variable $arg w "tixVariable:TraceProc $w"]
  51.     }
  52.  
  53.     return $changed
  54. }
  55.  
  56. proc tixVariable:UpdateVariable {w} {
  57.     upvar #0 $w data
  58.  
  59.     if {$data(-variable) != {}} {
  60.     uplevel #0 \
  61.         [list trace vdelete  $data(-variable) w "tixVariable:TraceProc $w"]
  62.     uplevel #0 \
  63.         [list set $data(-variable) $data(-value)]
  64.     uplevel #0 \
  65.         [list trace variable $data(-variable) w "tixVariable:TraceProc $w"]
  66.  
  67.     # just in case someone has another trace and restricted my change
  68.     #
  69.     set data(-value) [uplevel #0 set [list $data(-variable)]]
  70.     }
  71. }
  72.  
  73. proc tixVariable:TraceProc {w name1 name2 op} {
  74.  
  75.     if {$name2 == {}} {
  76.     if [catch {$w config -value [uplevel #0 set [list $name1]]} err] {
  77.         uplevel #0 set [list $name1] [list [$w cget -value]]
  78.         error $err
  79.     }
  80.     } else {
  81.     if [catch {$w config -value [uplevel #0 set [list $name1\($name2\)]]} err ] {
  82.         uplevel #0 set [list $name1\($name2\)] [list [$w cget -value]]
  83.         error $err
  84.     }
  85.     }
  86. }
  87.  
  88. proc tixVariable:DeleteVariable {w} {
  89.     upvar #0 $w data
  90.  
  91.     # Must delete the trace command of the -variable
  92.     #
  93.     if {$data(-variable) != {}} {
  94.     uplevel #0 \
  95.         [list trace vdelete $data(-variable) w "tixVariable:TraceProc $w"]
  96.     }
  97. }
  98.